package io.geekidea.boot.util.baiwangApi;

import com.baiwang.open.client.BWRestClient;
import com.baiwang.open.client.IBWClient;
import com.baiwang.open.client.login.BopLoginClient;
import com.baiwang.open.client.login.BopLoginResponse;
import com.baiwang.open.client.login.PasswordLoginClient;
import com.baiwang.open.client.login.PasswordLoginConfig;
import com.baiwang.open.entity.request.ImageInvoicesRecogcollectRequest;
import com.baiwang.open.entity.request.SOutputinvoicePushRequest;
import com.baiwang.open.entity.request.node.ImageInvoicesRecogcollectFileBase64Info;
import com.baiwang.open.entity.response.ImageInvoicesRecogcollectResponse;
import com.baiwang.open.entity.response.SOutputinvoicePushResponse;
import com.baiwang.open.exception.BWOpenException;
import io.geekidea.boot.config.baiwang.BaiwangConfig;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 发票查验
 */
@Component
public class Recogcollect {

    static String url = BaiwangConfig.getUrl();
    static String appKey = BaiwangConfig.getAppKey();
    static String userName = BaiwangConfig.getUserName();
    static String password = BaiwangConfig.getPassword();
    static String appSecret = BaiwangConfig.getAppSecret();
    static String userSalt = BaiwangConfig.getUserSalt();

    public static void recogcollectInvoice(
            List<ImageInvoicesRecogcollectFileBase64Info>  filesMap
    ) throws BWOpenException {
        try {
            String requestId = UUID.randomUUID().toString(); //自定义请求唯一标识
            PasswordLoginConfig loginConfig = new PasswordLoginConfig();
            loginConfig.setUrl(url);
            loginConfig.setClientId(appKey);
            loginConfig.setClientSecret(appSecret);
            loginConfig.setUsername(userName);
            loginConfig.setPassword(password);
            loginConfig.setUserSalt(userSalt);
            BopLoginClient loginClient = new PasswordLoginClient(loginConfig);
            // 获取token, token相关说明请参考 文档中心 -> 用户授权说明
            BopLoginResponse loginResponse = loginClient.login();
            // 重载方法，可以传自定义请求唯一标识
            // BopLoginResponse loginResponse = loginClient.login(requestId);
            String token = loginResponse.getResponse().getAccessToken();
            ImageInvoicesRecogcollectRequest request = new ImageInvoicesRecogcollectRequest();
            request.setCollectWay(0);

//            List<ImageInvoicesRecogcollectFileBase64Info> filesMap = new
//                    ArrayList<ImageInvoicesRecogcollectFileBase64Info>();
//            ImageInvoicesRecogcollectFileBase64Info imageInvoicesRecogcollectFileBase64Info = new
//                    ImageInvoicesRecogcollectFileBase64Info();
//            imageInvoicesRecogcollectFileBase64Info.setFileName("");
//            imageInvoicesRecogcollectFileBase64Info.setFileBase64("");
//            filesMap.add(imageInvoicesRecogcollectFileBase64Info);

            request.setFilesMap(filesMap);//必填 List<ImageInvoicesRecogcollectFileBase64Info>,图片或源文件的 base64及文件名的集 合
            request.setOrgCode("");//发票归属机构代码
            request.setSignStatus(1);//发票签收状态【默认0】(0-未签收,1-已签收）
            request.setUserAccount(""); // 必填 用户账号
            request.setCollectInvUseType("");//业务系统代码。当web页面-基础应用设 置中的“采集必填业 务系统”开关开启后，此字段必填
            request.setUploadMode(0);//发票上传模式
            request.setFolderId("");//票夹id
            request.setIsSave(0);//是否入库（0：入库，1：不入库。不填时 默认为0）
            IBWClient client = new BWRestClient(url, appKey, appSecret); // 初始化一个客户端
            //IBWClient client = new BWRestClient(url, appKey, appSecret, loginClient); // 初始化一个客户端,构造函数中传入loginConfig对象参数可针对‘token错误’异常进行重试1次。
            ImageInvoicesRecogcollectResponse response = client.imageInvoices().recogcollect(request,
                    token);
            // 重载方法，可以传自定义请求唯一标识
            // ImageInvoicesRecogcollectResponse response = client.imageInvoices().recogcollect(request,oken, requestId);
            System.out.println(response);
        } catch (BWOpenException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            System.out.println(e.getSubCode());
            System.out.println(e.getSubMessage());
        }
    }
}